{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Ask 询问\n",
    "\n",
    "原文: https://docs.sympy.org/latest/modules/assumptions/ask.html\n",
    "\n",
    "模块，用于查询关于假设的 SymPy 对象。\n",
    "\n",
    "## 类 class sympy.assumptions.ask.AssumptionKeys\n",
    "\n",
    "该类通过 ask 包含所有受支持的键。应该通过 `sympy.Q` 实例访问它。\n",
    "\n",
    "### 方法 sympy.assumptions.ask.ask(proposition, assumptions=True, context={}{})\n",
    "\n",
    "用假设评价命题的函数。\n",
    "\n",
    "\n",
    "#### 参数 proposition: Any boolean expression \n",
    "\n",
    "命题: 任何布尔表达式。\n",
    "\n",
    "命题将被求值为布尔值。如果这不是 `AppliedPredicate`，它将被 `Q.is_true` 包装。\n",
    "\n",
    "#### 参数 assumptions : Any boolean expression, optional.\n",
    "\n",
    "假设: 任何布尔表达式，可选。\n",
    "\n",
    "局部假设来评估命题。\n",
    "\n",
    "#### 参数 context : AssumptionsContext, optional.\n",
    "\n",
    "上下文: AssumptionsContext，可选。\n",
    "\n",
    "默认假设来评估命题。默认情况下，是 `sympy.assumptions.global_assumptions`变量。\n",
    "\n",
    "#### 返回 True, False, or None\n",
    "\n",
    "真，假，还是没有\n",
    "\n",
    "#### 异常\n",
    "\n",
    "TypeError : proposition or assumptions is not valid logical expression.\n",
    "\n",
    "类型错误: 命题或假设不是有效的逻辑表达式。\n",
    "\n",
    "ValueError : assumptions are inconsistent.\n",
    "\n",
    "值错误: 假设是不一致的。\n",
    "\n",
    "#### 解释\n",
    "\n",
    "This function evaluates the proposition to True or False if the truth value can be determined. If not, it returns None.\n",
    "\n",
    "如果可以确定真值，则此函数将命题计算为 `True` 或 `False`。如果没有，则返回 `None`。\n",
    "\n",
    "It should be discerned from refine() which, when applied to a proposition, simplifies the argument to symbolic Boolean instead of Python built-in True, False or None.\n",
    "\n",
    "它应该从 `refine()`中辨别出来，`refine()`应用于一个命题时，将参数简化为符号布尔值，而不是 Python 内置的 `True`、 `False` 或 `None`。\n",
    "\n",
    "#### 语法 ask(proposition)\n",
    "\n",
    "在全局假设语境中评价命题。\n",
    "\n",
    "#### 语法 ask(proposition, assumptions)\n",
    "\n",
    "在全局假设上下文中评价假设的命题。\n",
    "\n",
    "#### 例子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "from sympy import ask, Q, pi\n",
    "from sympy.abc import x, y\n",
    "print(ask(Q.rational(pi)))\n",
    "print(ask(Q.even(x*y), Q.even(x) & Q.integer(y)))\n",
    "print(ask(Q.prime(4*x), Q.integer(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果不能确定真值，则不返回真值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(ask(Q.odd(3*x))) # cannot determine unless we know x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果假设不一致，则引发 ValueError。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "inconsistent assumptions Q.odd(x) & Q.even(x)",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-e3bb329dace3>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mask\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mQ\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minteger\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mQ\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0meven\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m&\u001b[0m \u001b[0mQ\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0modd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m/opt/anaconda3/lib/python3.8/site-packages/sympy/assumptions/ask.py\u001b[0m in \u001b[0;36mask\u001b[0;34m(proposition, assumptions, context)\u001b[0m\n\u001b[1;32m    424\u001b[0m     \u001b[0;31m# check the satisfiability of given assumptions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    425\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mlocal_facts\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclauses\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0msatisfiable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0menc_cnf\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mFalse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 426\u001b[0;31m         \u001b[0;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"inconsistent assumptions %s\"\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0massumptions\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    427\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    428\u001b[0m     \u001b[0;32mif\u001b[0m \u001b[0mlocal_facts\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclauses\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: inconsistent assumptions Q.odd(x) & Q.even(x)"
     ]
    }
   ],
   "source": [
    "ask(Q.integer(x), Q.even(x) & Q.odd(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 注释\n",
    "\n",
    "假设中的关系尚未实现，因此下面的结论不会给出有意义的结果。然而，这是一项正在进行的工作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "ask(Q.positive(x), x > 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 参见 sympy.assumptions.refine.refine\n",
    "\n",
    "使用假设进行简化。如果真值不能确定，命题就不能简化为无。\n",
    "\n",
    "## 类 sympy.assumptions.ask.register_handler(key, handler)\n",
    "\n",
    "在 ask system. key 中注册一个处理程序必须是一个字符串，处理程序必须是从 `AskHandler` 继承的类。\n",
    "\n",
    "从版本1.8开始就不推荐使用多分派处理程序。\n",
    "\n",
    "## 类 sympy.assumptions.ask.remove_handler(key, handler)\n",
    "\n",
    "从 ask 系统中移除处理程序。语法与 `register_handler` 相同\n",
    "\n",
    "从版本1.8开始就不推荐使用多分派处理程序。"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "40d3a090f54c6569ab1632332b64b2c03c39dcf918b08424e98f38b5ae0af88f"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
